package com.un.ebs.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.un.ebs.core.enums.PowerTypeEnum;
import com.un.ebs.core.util.SecurityUtils;
import com.un.ebs.core.util.StringUtils;
import com.un.ebs.core.util.WebResult;
import com.un.ebs.core.util.WebUtils;
import com.un.ebs.sys.domain.AppRole;
import com.un.ebs.sys.domain.AppRolePowerExt;
import com.un.ebs.sys.domain.AppRoleRuleExt;
import com.un.ebs.sys.domain.AppUserRoleExt;
import com.un.ebs.sys.dto.AppRoleAddDto;
import com.un.ebs.sys.dto.AppRoleSetPowerDto;
import com.un.ebs.sys.dto.AppRoleSetRuleDto;
import com.un.ebs.sys.dto.AppRoleUpdateDto;
import com.un.ebs.sys.mapper.AppRolePowerExtMapper;
import com.un.ebs.sys.mapper.AppRoleRuleExtMapper;
import com.un.ebs.sys.mapper.AppUserRoleExtMapper;
import com.un.ebs.sys.service.AppRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author liubo
 * @since 2019-09-15
 */
@RestController
@RequestMapping("/appRole")
@Api(value = "sys-app-role", description = "角色")
public class AppRoleController {

    @Autowired
    private AppRoleService service;

    @Autowired
    private AppRolePowerExtMapper appRolePowerExtMapper;

    @Autowired
    private AppRoleRuleExtMapper appRoleRuleExtMapper;

    @Autowired
    private AppUserRoleExtMapper appUserRoleExtMapper;

    @PostMapping("add")
    @ApiOperation(value = "add", notes = "添加")
    public WebResult save(@RequestBody @Valid AppRoleAddDto req) {
        service.add(req);
        return WebUtils.success();
    }

    @PostMapping("update")
    @ApiOperation(value = "update", notes = "修改")
    public WebResult update(@RequestBody @Valid AppRoleUpdateDto req) {
        if (StringUtils.isNullOrEmpty(req.getId())) {
            service.add(req);
        } else {
            service.update(req);
        }
        return WebUtils.success();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "getById", notes = "根据主键获取单个 AppRole")
    public WebResult getById(@PathVariable("id") String id) {
        return WebUtils.success(service.queryById(id));
    }

    @GetMapping("list/{appId}")
    @ApiOperation(value = "list", notes = "获取指定app的角色列表")
    public WebResult<List<AppRole>> list(@PathVariable(name = "appId", required = false) String appId) {
        if (StringUtils.isNullOrEmpty(appId)) {
            appId = SecurityUtils.getUserContent().getAppId();
        }
        LambdaQueryWrapper<AppRole> lq = Wrappers.lambdaQuery();
        lq.eq(AppRole::getAppId, appId);
        switch (SecurityUtils.getUserContent().getType()) {
            case DEVELOPER:
                lq.isNull(AppRole::getTenantId);
                //lq.or().eq(AppRole::getTenantId, SecurityUtils.getUserContent().getTenantId());
                break;
            case USER:
                lq.or().eq(AppRole::getTenantId, SecurityUtils.getUserContent().getTenantId());
                break;
            case TENANT_ADMIN:
                lq.or().eq(AppRole::getTenantId, SecurityUtils.getUserContent().getTenantId());
                break;
            default:
                lq.eq(AppRole::getTenantId, SecurityUtils.getUserContent().getTenantId());
                break;
        }
        return WebUtils.success(service.list(lq));
    }


    @GetMapping("listPower/{roleId}")
    @ApiOperation(value = "listPower", notes = "")
    public WebResult listPower(@PathVariable("roleId") String roleId) {
        LambdaQueryWrapper<AppRolePowerExt> lq = Wrappers.lambdaQuery();
        lq.eq(AppRolePowerExt::getRoleId, roleId);
        lq.eq(AppRolePowerExt::getType, PowerTypeEnum.Menu.getValue());
        lq.orderByAsc(AppRolePowerExt::getSort);
        return WebUtils.success(appRolePowerExtMapper.selectList(lq));
    }

    @GetMapping("listRule/{roleId}")
    @ApiOperation(value = "listRule", notes = "")
    public WebResult listRule(@PathVariable("roleId") String roleId) {
        LambdaQueryWrapper<AppRoleRuleExt> lq = Wrappers.lambdaQuery();
        lq.eq(AppRoleRuleExt::getRoleId, roleId);
        lq.orderByAsc(AppRoleRuleExt::getSort);
        return WebUtils.success(appRoleRuleExtMapper.selectList(lq));
    }

    @GetMapping("listByUser/{userId}")
    @ApiOperation(value = "listByUser", notes = "")
    public WebResult listByUser(@PathVariable("userId") String userId) {
        LambdaQueryWrapper<AppUserRoleExt> lq = Wrappers.lambdaQuery();
        lq.eq(AppUserRoleExt::getUserId, userId);
        lq.eq(AppUserRoleExt::getTenantId, SecurityUtils.getTenantId());
        return WebUtils.success(appUserRoleExtMapper.selectList(lq));
    }

    @PostMapping("delete/{id}")
    @ApiOperation(value = "delete", notes = "根据主键删除单个 AppRole")
    public WebResult delete(@PathVariable("id") String id) {
        service.delete(id);
        return WebUtils.success();
    }

    @PostMapping("changeSort")
    @ApiOperation(value = "changeSort", notes = "修改 AppRole 的序号")
    public WebResult changeSort(@RequestParam String id, @RequestParam int index) {
        service.changeSort(id, index);
        return WebUtils.success();
    }

    @PostMapping("setPower/{roleId}")
    @ApiOperation(value = "setPower", notes = "设置角色权限")
    public WebResult setPower(@PathVariable("roleId") String roleId, @RequestBody List<AppRoleSetPowerDto> req) {
        service.setPower(roleId, req);
        return WebUtils.success();
    }

    @PostMapping("setResourcePower")
    @ApiOperation(value = "setResourcePower", notes = "设置角色的动态资源权限")
    public WebResult setResourcePower(@RequestParam String roleId, @RequestParam String resourceId, @RequestParam Boolean checked, @RequestParam PowerTypeEnum powerType) {
        service.setResourcePower(roleId, resourceId, checked, powerType);
        return WebUtils.success();
    }

    @PostMapping("setRule/{roleId}")
    @ApiOperation(value = "setRule", notes = "设置角色规则")
    public WebResult setRule(@PathVariable("roleId") String roleId, @RequestBody List<AppRoleSetRuleDto> req) {
        service.setRule(roleId, req);
        return WebUtils.success();
    }

}